﻿using System;
using System.Collections.Generic;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BaseSet.Data;
using BaseSet.Logic;
using BaseSet.Entity;
using BaseSet.Framework;
using BaseSet.Entity.Common;
using BaseSet.Entity.Common.Text;
using BaseSet;
using System.Data;

namespace WebSite.Admin.BlogManager
{
    public partial class BlogReportList : BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                BindReportType();
                LoadContent(false);

                // 从BasePage中得到
                PageLiteral.Text = BuildPageLinks();
            }
        }


        #region 加载列表
        private void LoadContent(bool mark)
        {

            //设置控件属性，从BasePage中得到
            PageConfig(BlogUPager);

            LoadCondition();

            string reporttypeid = ddlReportType.SelectedValue;

            //List<Member> list = new List<Member();
            BlogReportHelper brhelper = new BlogReportHelper();
            Criteria c = new Criteria();

            c.Add(CriteriaType.Equals, "Flag", 0);
            if (reporttypeid != "-1")
            {
                c.Add(CriteriaType.Equals, "ReportTypeID", reporttypeid);
            }

            BlogUPager.ItemCount = brhelper.QueryBlogReportCountBySearch(c);

            // 以下的值从BasePage中得到
            BlogUPager.UrlFormat = UrlFormat;
            BlogUPager.PrefixText = GetPreFixText(BlogUPager);

            if (mark)
            {
                PagerInit(BlogUPager);
            }

            string[] fields = new string[] { "ID", "BlogID", "BlogUsersID", "ReportTypeID", "ReportContent", "Flag", "CreateDate" };

            DataTable dt = new DataTable();
            dt = brhelper.QueryBlogReportBySearchToTable(c, new Order[] { new Order("CreateDate", OrderMode.Desc) }, BlogUPager.Begin - 1, BlogUPager.Count, fields);

            dt.Columns.Add(new DataColumn("Title"));
            dt.Columns.Add(new DataColumn("ReportType"));
            dt.Columns.Add(new DataColumn("BlogUserNickName"));

            if (dt != null && dt.Rows.Count > 0)
            {
                foreach (DataRow dr in dt.Rows)
                {
                    string blogid = dr["BlogID"] + "";
                    //加载博客标题
                    Blog binfo = new Blog();
                    BlogHelper bhelper = new BlogHelper();
                    binfo = bhelper.GetBlog(blogid);
                    dr["Title"] = binfo.Title;

                    string bloguserid = dr["BlogUsersID"] + "";
                    //加载博客用户昵称
                    Users uinfo = new Users();
                    UsersHelper uhelper = new UsersHelper();
                    uinfo = uhelper.GetUsers(bloguserid);
                    if (uinfo != null)
                    {
                        dr["BlogUserNickName"] = uinfo.NickName;
                    }
                    else
                    {
                        dr["BlogUserNickName"] = "";
                    }

                    string getreporttype = dr["ReportTypeID"] + "";
                    //加载举报类型
                    ReportType rtinfo = new ReportType();
                    ReportTypeHelper rthelper = new ReportTypeHelper();
                    rtinfo = rthelper.GetReportType(getreporttype);
                    dr["ReportType"] = rtinfo.ReportTypeName;
                }
            }

            rptBlog.DataSource = dt;
            rptBlog.DataBind();
        }
        #endregion

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            SetCondition();
            LoadContent(true);
        }

        #region 列表事件
        protected void rptBlog_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            HiddenField hidBlogID = (HiddenField)e.Item.FindControl("hidBlogID");

            string blogid = hidBlogID.Value;

            if (e.CommandName == "delete")//屏蔽博客
            {
                Blog binfo = new Blog();
                BlogHelper bhelper = new BlogHelper();
                binfo = bhelper.GetBlog(blogid);
                binfo.Flag = -9;
                bhelper.EditBlog(binfo);
                //将举报该博客的信息置为无效
                BlogReportHelper brhelper = new BlogReportHelper();
                brhelper.BlogReportEditByBlogID(blogid);
                Tools.Alert("屏蔽博客成功！");
            }

            LoadContent(false);
        }
        #endregion

        #region 查询条件设置
        private void SetCondition()
        {
            string reporttypeid = ddlReportType.SelectedValue;

            //将查询条件放入Session中
            Session["QueryList"] = reporttypeid + "@" + "BlogReportList";

        }

        private void LoadCondition()
        {
            string condition = Session["QueryList"] + "";

            string[] conditionAll = condition.Split('@');
            if (conditionAll.Length == 2)
            {
                string page = conditionAll[1];
                if (page == "BlogReportList")
                {
                    ddlReportType.SelectedValue = conditionAll[0];
                }
            }
        }
        #endregion


        #region 加载举报类别
        private void BindReportType()
        {
            ReportTypeHelper rhelper = new ReportTypeHelper();
            Criteria c = new Criteria();
            c.Add(CriteriaType.Equals, "Flag", 0);
            List<ReportType> clist = rhelper.QueryReportTypeBySearch(c, null);
            ddlReportType.DataSource = clist;
            ddlReportType.DataTextField = "ReportTypeName";
            ddlReportType.DataValueField = "ID";
            ddlReportType.DataBind();

            ListItem li = new ListItem();
            li.Value = "-1";
            li.Text = "-请选择-";
            ddlReportType.Items.Insert(0, li);
        }
        #endregion
    }
}